﻿using System;

namespace EpubSqueaker
{
	public class FileReference
	{
		private string filePath;
		private string opfPath;

		public FileReference(string filePathToSet)
		{
			if (string.IsNullOrEmpty(filePathToSet))
				throw new ArgumentNullException("filePathToSet");

			opfPath = "";
			filePath = filePathToSet;
		}

		public FileReference(string opfPathToSet, string filePathToSet)
		{
			if (string.IsNullOrEmpty(filePathToSet))
				throw new ArgumentNullException("filePathToSet");

			if (string.IsNullOrEmpty(opfPathToSet))
				opfPath = "";
			else if (opfPathToSet.EndsWith("/", StringComparison.OrdinalIgnoreCase))
				opfPath = opfPathToSet;
			else
				opfPath = opfPathToSet + "/";

			filePath = filePathToSet;
		}

		/// <summary>
		/// Full path of the file.
		/// </summary>
		public string FullPath
		{
			get
			{
				return opfPath + filePath;
			}
		}

		/// <summary>
		/// Full path of the file relative to the specified OPF file (if any).
		/// </summary>
		public string FullPathFromOpf
		{
			get
			{
				return filePath;
			}
		}

		/// <summary>
		/// Path to the OPF file.
		/// </summary>
		public string OpfPath
		{
			get
			{
				return opfPath;
			}
		}

		/// <summary>
		/// Directory path to the file. If not blank, omits trailing slash.
		/// </summary>
		public string DirectoryName
		{
			get
			{
				return opfPath + System.IO.Path.GetDirectoryName(filePath);
			}
		}

		/// <summary>
		/// Directory path to the file relative to the specified OPF file (if any). If not blank, omits trailing slash.
		/// </summary>
		public string DirectoryNameFromOpf
		{
			get
			{
				return System.IO.Path.GetDirectoryName(filePath);
			}
		}

		/// <summary>
		/// Directory path to the file. Includes trailing forward slash (method is designed for URL use) if not blank.
		/// </summary>
		public string DirectoryPath
		{
			get
			{
				string toReturn = System.IO.Path.GetDirectoryName(opfPath + filePath);
				if (string.IsNullOrEmpty(toReturn))
					return "";
				else
					return toReturn + "/"; // Used in URLs, should always be a slash.
			}
		}

		/// <summary>
		/// Directory path to the file relative to the specified OPF file (if any). Includes trailing forward slash (method is designed for URL use) if not blank.
		/// </summary>
		public string DirectoryPathFromOpf
		{
			get
			{
				string toReturn = System.IO.Path.GetDirectoryName(filePath);
				if (string.IsNullOrEmpty(toReturn))
					return "";
				else
					return toReturn + "/"; // Used in URLs, should always be a slash.
			}
		}

		/// <summary>
		/// Name of the file within the directory.
		/// </summary>
		public string FileName
		{
			get
			{
				return System.IO.Path.GetFileName(filePath);
			}
		}

		/// <summary>
		/// Name of the file within the directory. Quoted if it contains spaces.
		/// </summary>
		public string FileNameForZip
		{
			get
			{
				string toReturn = FileName;
				if (toReturn.Contains(" "))
				{
					toReturn = '"' + toReturn + '"';
				}
				return toReturn;
			}
		}

		/// <summary>
		/// File name of the file (without extension).
		/// </summary>
		public string FileNameWithoutExtension
		{
			get
			{
				return System.IO.Path.GetFileNameWithoutExtension(filePath);
			}
		}

		/// <summary>
		/// File extension of the file (includes preceding dot).
		/// </summary>
		public string Extension
		{
			get
			{
				return System.IO.Path.GetExtension(filePath);
			}
		}
	}
}
